package com.sf.fy.uav.applet.utils;

import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

/**
 * @author 01430312
 */
public class RSAUtils {
    private static final String ALGORITHM = "RSA";

    // 加密方法
    public static String encrypt(String plainText, PublicKey publicKey) throws Exception {
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] encryptedBytes = cipher.doFinal(plainText.getBytes());
        return Base64.getEncoder().encodeToString(encryptedBytes);
    }


    // 解密方法
    public static String decrypt(String encryptedText) throws Exception {
        String privateKeyString = "MIIJQQIBADANBgkqhkiG9w0BAQEFAASCCSswggknAgEAAoICAQDCWOLA6pNY2uwm" + "FZ17rcEWdpdqyx7Qa1l8l3te3+mzZo9XoSls2979DttIlmgvcojBNWxFvd2HPxyf" + "rl/kGs4Ax4f8rPhY8K+CCb/kS3icWXaxec1261ZYSCOmuR+AhNGEtVElG8aJOFhJ" + "zLGK3UvOy7hafG3I4haOVWuNJahp35Km/DKTO4kGRBfSxzqzmHo3ehw1sJNvKB6n" + "XA7iQdN8H5vgNdDIrOG9fDGHVzp5c8IAfTxKWb5n/iWvXFFPJmlzfu3oyEMFaTpm" + "t+wYmpAVXIfA6mVER2GKBrKart6Pkf7PxLN4SLhSJ0I8fOQU+fvgYZG0141q7oz5" + "aKN+BuwHSa3ygdaaK2UH81yvcUTKIrxiKTG4QIBOo1nfYTJ+TLRI2oQuJfBoQYMz" + "OuxF5RMJIbg9wVq5M/ut3efPJKsk3+3RjixiDSAidlzrBofpGt9ui74NVH4zpXi+" + "W0ZwNFHvGXhsnGpCtqJvWYlkn9ydCvlLkq/QF6adI9sXLL66Hhv4njMyYpj2LCeJ" + "W6nZ7JXSUL46J7vDy3G5F5y4j8skMJfEV7IgEi6uxh1tHqBaHBB1v8cMODWgvsIm" + "PR5d2dBrqEx+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CI" + "pym73RUW9vOmpE2NgnMtGy9XnRPa399qDBmv80R4BrzRGyyqYGTokWzrsKgG0kfh" + "Q+ScUbgHaVQKRANmARfdngPrvsHYjyJkxTy6zj4acBIao1ojm4zyFTaTIUFfi27Z" + "4QKCAQB9wNz0KF9xBzIjk0X/n7hRNg+QK5exD5pgtjmE11Ie40PpdHemT/bIl5Zt" + "Er3gP5kuXShkIwzHkVCppDUL3bc2qLiy2czfu2S1l1fjZNyTjgasj5aFN2GAgKt7" + "WvfHZQTKLdFdzX2atgwls12KKl+8CdZPZU5jKZhdnOVhVBcIyxDxzp8I0KFGaZrL" + "Ptr7Iigck4queW9Ja9jbnjs2af7ojjcZ25BS6clQJQGqnWpyYUqIq+tvH2QZCgZd" + "uaaSjzNZtAnilaOVBZILiOU2mLtcaF4rksq2RFiX5zINypXPBrp8eoc82e7FZcUj" + "j0pjGHdA8a2VORg4ocHoXuHSa4IC";
        // 从字符串格式的公钥和私钥创建公钥和私钥对象
        try {
            PrivateKey privateKey = getPrivateKeyFromString(privateKeyString);
            byte[] encryptedBytes = Base64.getDecoder().decode(encryptedText);
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
            byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
            return new String(decryptedBytes);
        } catch (Exception e) {
            throw e;
        }
    }

    //测试一下
    public static void main(String[] args) throws Exception {

        // 公钥和私钥的字符串格式
        String publicKeyString = "MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAwljiwOqTWNrsJhWde63B" + "FnaXasse0GtZfJd7Xt/ps2aPV6EpbNve/Q7bSJZoL3KIwTVsRb3dhz8cn65f5BrO" + "AMeH/Kz4WPCvggm/5Et4nFl2sXnNdutWWEgjprkfgITRhLVRJRvGiThYScyxit1L" + "zsu4WnxtyOIWjlVrjSWoad+SpvwykzuJBkQX0sc6s5h6N3ocNbCTbygep1wO4kHT" + "fB+b4DXQyKzhvXwxh1c6eXPCAH08Slm+Z/4lr1xRTyZpc37t6MhDBWk6ZrfsGJqQ" + "FVyHwOplREdhigaymq7ej5H+z8SzeEi4UidCPHzkFPn74GGRtNeNau6M+Wijfgbs" + "B0mt8oHWmitlB/Ncr3FEyiK8YikxuECATqNZ32Eyfky0SNqELiXwaEGDMzrsReUT" + "CSG4PcFauTP7rd3nzySrJN/t0Y4sYg0gInZc6waH6Rrfbou+DVR+M6V4vltGcDRR" + "7xl4bJxqQraib1mJZJ/cnQr5S5Kv0BemnSPbFyy+uh4b+J4zMmKY9iwniVup2eyV" + "0lC+Oie7w8txuRecuI/LJDCXxFeyIBIursYdbR6gWhwQdb/HDDg1oL7CJj0eXdnQ" + "a6hMfnS3aiGanrM6inwyC97ZWUqlRJ1MqS08S5bMiSpNzwPNAKuHAfel1RGkljDq" + "iKkhZv5BubDf/xmRCfPTWmcCAwEAAQ==";
        String privateKeyString = "MIIJQQIBADANBgkqhkiG9w0BAQEFAASCCSswggknAgEAAoICAQDCWOLA6pNY2uwm" + "FZ17rcEWdpdqyx7Qa1l8l3te3+mzZo9XoSls2979DttIlmgvcojBNWxFvd2HPxyf" + "rl/kGs4Ax4f8rPhY8K+CCb/kS3icWXaxec1261ZYSCOmuR+AhNGEtVElG8aJOFhJ" + "zLGK3UvOy7hafG3I4haOVWuNJahp35Km/DKTO4kGRBfSxzqzmHo3ehw1sJNvKB6n" + "XA7iQdN8H5vgNdDIrOG9fDGHVzp5c8IAfTxKWb5n/iWvXFFPJmlzfu3oyEMFaTpm" + "t+wYmpAVXIfA6mVER2GKBrKart6Pkf7PxLN4SLhSJ0I8fOQU+fvgYZG0141q7oz5" + "aKN+BuwHSa3ygdaaK2UH81yvcUTKIrxiKTG4QIBOo1nfYTJ+TLRI2oQuJfBoQYMz" + "OuxF5RMJIbg9wVq5M/ut3efPJKsk3+3RjixiDSAidlzrBofpGt9ui74NVH4zpXi+" + "W0ZwNFHvGXhsnGpCtqJvWYlkn9ydCvlLkq/QF6adI9sXLL66Hh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TWJoMbZGxXCpz1hgXL4EeGsfjHYn6Aj9ae" + "03ZbpUUbEplSGwmva4S0nIic+q403ul0oTUypOFUl2GLKBahlDLtpcYQ4UlVtFCI" + "pym73RUW9vOmpE2NgnMtGy9XnRPa399qDBmv80R4BrzRGyyqYGTokWzrsKgG0kfh" + "Q+ScUbgHaVQKRANmARfdngPrvsHYjyJkxTy6zj4acBIao1ojm4zyFTaTIUFfi27Z" + "4QKCAQB9wNz0KF9xBzIjk0X/n7hRNg+QK5exD5pgtjmE11Ie40PpdHemT/bIl5Zt" + "Er3gP5kuXShkIwzHkVCppDUL3bc2qLiy2czfu2S1l1fjZNyTjgasj5aFN2GAgKt7" + "WvfHZQTKLdFdzX2atgwls12KKl+8CdZPZU5jKZhdnOVhVBcIyxDxzp8I0KFGaZrL" + "Ptr7Iigck4queW9Ja9jbnjs2af7ojjcZ25BS6clQJQGqnWpyYUqIq+tvH2QZCgZd" + "uaaSjzNZtAnilaOVBZILiOU2mLtcaF4rksq2RFiX5zINypXPBrp8eoc82e7FZcUj" + "j0pjGHdA8a2VORg4ocHoXuHSa4IC";
        // 从字符串格式的公钥和私钥创建公钥和私钥对象
        PublicKey publicKey = getPublicKeyFromString(publicKeyString);
        PrivateKey privateKey = getPrivateKeyFromString(privateKeyString);

        // 要加密的明文
        String plainText = "Hello, RSA!";
        // 使用公钥进行加密
        String encryptedText = encrypt(plainText, publicKey);
        // 打印加密结果
        System.out.println("Encrypted: " + encryptedText);

    }

    // 从字符串格式的公钥创建 PublicKey 对象
    public static PublicKey getPublicKeyFromString(String publicKeyString) throws Exception {
        byte[] publicKeyBytes = Base64.getDecoder().decode(publicKeyString);
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
        return keyFactory.generatePublic(keySpec);
    }

    // 从字符串格式的私钥创建 PrivateKey 对象
    public static PrivateKey getPrivateKeyFromString(String privateKeyString) throws Exception {
        byte[] privateKeyBytes = Base64.getDecoder().decode(privateKeyString);
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
        return keyFactory.generatePrivate(keySpec);
    }

}